package cn.iocoder.yudao.module.handguard.service.processrecord;

import cn.iocoder.yudao.module.handguard.dal.dataobject.applicationrecord.ApplicationRecordDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.applicationrecord.ApplicationRecordMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.app.processrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.processrecord.ProcessRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.processrecord.ProcessRecordConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.processrecord.ProcessRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 用户申请贷款流程处理记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProcessRecordServiceImpl implements ProcessRecordService {

    @Resource
    private ProcessRecordMapper processRecordMapper;

    @Resource
    private ApplicationRecordMapper applicationRecordMapper;

    @Override
    public Long createProcessRecord(ProcessRecordCreateReqVO createReqVO) {
        // 插入
        ProcessRecordDO processRecord = ProcessRecordConvert.INSTANCE.convert(createReqVO);
        processRecordMapper.insert(processRecord);
        // 返回
        return processRecord.getId();
    }

    @Override
    public void updateProcessRecord(ProcessRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateProcessRecordExists(updateReqVO.getId());
        // 更新
        ProcessRecordDO updateObj = ProcessRecordConvert.INSTANCE.convert(updateReqVO);
        processRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessRecord(Long id) {
        // 校验存在
        validateProcessRecordExists(id);
        // 删除
        processRecordMapper.deleteById(id);
    }

    private void validateProcessRecordExists(Long id) {
        if (processRecordMapper.selectById(id) == null) {
            throw exception(PROCESS_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ProcessRecordDO getProcessRecord(Long id) {
        return processRecordMapper.selectById(id);
    }

    @Override
    public List<ProcessRecordDO> getProcessRecordList(Long orderId) {
        ApplicationRecordDO applicationRecordDO = applicationRecordMapper.selectById(orderId);
        List<ProcessRecordDO> recordDOList = processRecordMapper.selectList("application_id",orderId);
        for (ProcessRecordDO recordDO : recordDOList) {
            recordDO.setApplyState(applicationRecordDO.getApplyState());
        }
        return recordDOList;
    }

    @Override
    public PageResult<ProcessRecordDO> getProcessRecordPage(ProcessRecordPageReqVO pageReqVO) {
        return processRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProcessRecordDO> getProcessRecordList(ProcessRecordExportReqVO exportReqVO) {
        return processRecordMapper.selectList(exportReqVO);
    }

}
